package com.lagou.kafka.demo.consumer;

import com.lagou.kafka.demo.deserializer.UserDeserializer;
import com.lagou.kafka.demo.entity.User;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.*;
import java.util.function.Consumer;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 消费者
 * @date 2022/6/6 21:02
 */
public class UserConsumer {


    public static void main(String[] args) {
        Properties configs = new Properties();
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.106.130:9092");
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, UserDeserializer.class);
        // 设置自定义的反序列化器
        configs.put(ConsumerConfig.GROUP_ID_CONFIG,"user_consumer1");
        configs.put(ConsumerConfig.CLIENT_ID_CONFIG,"consumer_id");
        configs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        KafkaConsumer<String, User> consumer = new KafkaConsumer<>(configs);
        // 订阅主题
        consumer.subscribe(Collections.singleton("tp_user_01"));
        ConsumerRecords<String, User> records = consumer.poll(Long.MAX_VALUE);
        records.forEach(stringUserConsumerRecord -> System.out.println(stringUserConsumerRecord.value()));

        consumer.close();

    }
}
